package com.itheima.mobileController;

import com.aliyuncs.exceptions.ClientException;
import com.itheima.constant.MessageConstant;
import com.itheima.constant.RedisMessageConstant;
import com.itheima.entity.Result;
import com.itheima.utils.SMSUtils;
import com.itheima.utils.ValidateCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

/**
 * <h3>health94_pareant</h3>
 * <p></p>
 *
 * @author : hhw
 * @date : 2020-03-29 11:03
 **/
@RestController
@RequestMapping("validate")
public class ValidateController {
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("sendValidateCode")
    public Result sendValidateCode(@RequestParam("telephone") String telephone) throws ClientException {

        try {
            Integer validateCode = ValidateCodeUtils.generateValidateCode(4);
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, String.valueOf(validateCode));
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_ORDER, 60*10, String.valueOf(validateCode));

            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
    @RequestMapping("loginValidateCode")
    public Result loginValidateCode(@RequestParam("telephone") String telephone) throws ClientException {

        try {
            Integer noticeCode = ValidateCodeUtils.generateValidateCode(4);
            SMSUtils.sendShortMessage(SMSUtils.VALIDATE_CODE, telephone, String.valueOf(noticeCode));
            jedisPool.getResource().setex(telephone+ RedisMessageConstant.SENDTYPE_LOGIN, 60*10, String.valueOf(noticeCode));
            return new Result(true, MessageConstant.SEND_VALIDATECODE_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.SEND_VALIDATECODE_FAIL);
        }
    }
}
